package com.lili.operate.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lili.model.operate.Article;
import com.lili.operate.mapper.ArticleMapper;
import com.lili.operate.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    ArticleMapper articleMapper;

    /**
     * 根据类别显示对应的所有的文章
     * @param map
     * @return
     */
    @Override
    public Page<Article> queryArticles(Map map) {
        String type = map.get("type").toString();
        int current = 1;
        int size = 3;
        if(map.get("current")!=null){
            current = Integer.parseInt(map.get("current").toString());

        }
        if (map.get("size")!=null){
            size = Integer.parseInt(map.get("size").toString());
        }
        Page<Article> page = new Page<>(current,size);
        QueryWrapper<Article> wrapper = new QueryWrapper<>();
        wrapper.eq("type",type).eq("delete_flag",0);
        articleMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 保存文章
     * @param article
     * @return
     */
    @Override
    public int saveArticle(Article article) {
        return articleMapper.insert(article);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public int deleteArticle(long id) {
        return articleMapper.deleteArticle(id);
    }

    /**
     * 更新
     * @param article
     * @return
     */
    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateById(article);
    }

    /**
     * 更新状态
     * @param id
     * @return
     */
    @Override
    public int statusArticle(long id,Byte status) {
        return articleMapper.statusArticle(id,status);
    }
}
